package ch.fhnw.qualiscan.application.modals;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.elements.BestPracticeElement;
import ch.fhnw.qualiscan.application.elements.BestPracticeForm;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.BestPractice;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

import eu.livotov.tpt.i18n.TM;


@SuppressWarnings("serial")
public class AddBestPractice extends Modal {
	
	BestPracticeForm bpf;
	Button saveButton;
	Button cancelButton;

	Survey survey;
	Domain domain;
	User user;
	DomainModel domainModel;
	BestPractice bp;
	SurveyUserRole sur;
	
	Button bestPracticeButton = null;
	
	public AddBestPractice(SurveyUserRole sur, Domain domain) {
		this.survey = sur.getSurvey();
		this.domainModel =  new DomainModel();
		this.domain = domain;
		this.sur = sur;
		
		
		user = (User) hibernateSession.getSession().get(User.class, userSession.getId());

		buildLayout();
	}
	
	
	public AddBestPractice(SurveyUserRole sur, Domain domain, Button bestPracticeButton) {
		this.survey = sur.getSurvey();
		this.domainModel =  new DomainModel();
		this.domain = domain;
		this.bestPracticeButton = bestPracticeButton;
		this.sur = sur;
		
		user = (User) hibernateSession.getSession().get(User.class, userSession.getId());

		buildLayout();
	}


	public void buildLayout() {
		String domainText = domainModel.getDomainText(domain, sessionHelper.getUser().getLanguage()).getText();
		this.setCaption(TM.get("bestpractice.caption", domainText));
		
		TabSheet tabSheet = new TabSheet(); 
		VerticalLayout tab1 = buildTab1(domainText);
		Panel tab2 = buildTab2();
		
		
		
		tabSheet.addTab(tab1, TM.get("bestpractice.tab1.title") ,null);
		tabSheet.addTab(tab2, TM.get("bestpractice.tab2.title"), null);
		
		addComponent(tabSheet);
	}


	private VerticalLayout buildTab1(String domainText) {
		
		VerticalLayout vLayout = new VerticalLayout();
		vLayout.setMargin(true);
		vLayout.setSpacing(true);
		
		bpf = new BestPracticeForm(domainText);
		
		bp = domainModel.getBestPracticeOfDomain(sur, domain);
		if (bp != null) {
			bpf.setValue(bp.getBestpractice(), bp.getTitle(), bp.getPublicpost());
		}
		
		saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				
				if (bp == null) {
					BestPractice newbp = new BestPractice();
					newbp.setBestpractice(bpf.getTextValue());
					newbp.setPublicpost(bpf.getPublicValue());
					newbp.setTitle(bpf.getTitleValue());
					newbp.setEditdate(new java.util.Date());
					newbp.setSurvey(survey);
					newbp.setUser(user);
					newbp.setSur(sur);
					
					domainModel.saveBestPracticeOfDomain(newbp, domain);
					if(!newbp.getTitle().equals("") && !newbp.getBestpractice().equals("")) {
						bestPracticeButton.setIcon(Config.ICON_EDITBESTPRACTICE);
						bestPracticeButton.setCaption(TM.get("bestpractice.edit"));
					}
				}
				else {
					bp.setBestpractice(bpf.getTextValue());
					
					bp.setPublicpost(bpf.getPublicValue());
					bp.setTitle(bpf.getTitleValue());
					bp.setEditdate(new java.util.Date());
					domainModel.updateBestPracticeOfDomain(bp);
					if(!bp.getTitle().equals("") && !bp.getBestpractice().equals("")) {
						bestPracticeButton.setIcon(Config.ICON_EDITBESTPRACTICE);
						bestPracticeButton.setCaption(TM.get("bestpractice.edit"));
					}
				}
				AddBestPractice abp = (AddBestPractice) event.getButton().getData();
				abp.close();
			}
		});
		saveButton.setData(this);
		saveButton.setIcon(Config.ICON_SAVE);
		
		cancelButton = new Button(TM.get("action.cancel"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AddBestPractice abp = (AddBestPractice) event.getButton().getData();
				abp.close();
			}
		});
		cancelButton.setData(this);
		cancelButton.setIcon(Config.ICON_CANCEL);
		
		HorizontalLayout buttonBar = new HorizontalLayout();
		buttonBar.setSpacing(true);
		buttonBar.addComponent(saveButton);
		buttonBar.addComponent(cancelButton);
		
		vLayout.addComponent(bpf);
		vLayout.addComponent(buttonBar);
		
		return vLayout;
		
	}
	
	private Panel buildTab2() {
		
		VerticalLayout vLayout = new VerticalLayout();
		vLayout.setMargin(true);
		vLayout.setSpacing(true);
		
		Panel panel = new Panel();
		panel.setHeight("410px");
		panel.setStyleName(Reindeer.PANEL_LIGHT);
		panel.addComponent(vLayout);
		
		
		List<BestPractice> bpList = domainModel.getBestPractices(survey, domain);
		for (int i = 0; i < bpList.size(); i++) {
			BestPracticeElement bestPractice = new BestPracticeElement(bpList.get(i));
			vLayout.addComponent(bestPractice);
		}
		
		return panel;
	}
	
}
